using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ADESSA.Entities;
using ADESSA.Business;
using FetCo.Data;
using FetCo.Framework;

public partial class Upgrade : System.Web.UI.Page
{
    #region Viarible
    public int NoCountStart
    {
        get { return Convert.ToInt32(ViewState["NoCountStart"]); }
        set { ViewState["NoCountStart"] = value; }
    }
    ADESSA.Entities.Company _Company = new ADESSA.Entities.Company();
    ADESSA.Entities.Member _Member = new ADESSA.Entities.Member();
    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            return;
        LoadShipFree();
        LoadGender();
        LoadMember();
    }
    #region Continues
    protected void linkNext_Click(object sender, EventArgs e)
    {
        _Member = MemberService.GetMember(long.Parse(Session["MemberId"].ToString()));
        Criteria cri = new Criteria();
        cri.add("[" + ADESSA.Entities.Company._Table + "].[" + CompanyColumns.MemberID + "]", _Member.MemberID, Condition.EQUAL, Condition.AND);
        CompanyCollection _CompanyCollection = CompanyService.GetCompanyListByParam("[" + ADESSA.Entities.Company._Table + "].[" + CompanyColumns.CompanyID + "]", OrderDirection.ASC, cri.Criter);
        if (_CompanyCollection.Count > 0)
        {
            _Company = _CompanyCollection[0];
        }
        _Member.FullName = txtNameMem.Value.Trim();
        _Member.Sex = int.Parse(ddlSex.SelectedValue.ToString());
        _Member.Other = Utilitys.GenerateRandomCode(12);
        //ShipFree For Member
        foreach (RepeaterItem item in this.rpt_ShipFree.Items)
        {
            RadioButton rdo = (RadioButton)item.FindControl("rdoFree");
            if (rdo.Checked == true)
            {
                HiddenField hidID = (HiddenField)item.FindControl("hidID");
                string strhidID = hidID.Value;
                int ID = int.Parse(strhidID);
                _Member.MembershipFeeID = ID;
            }
        }
        //End
        _Company.CompanyName = txtNameCom.Value.Trim();
        _Company.Address = txtConAdd.Value.Trim();
        _Company.ZipCode = txtZip.Value.Trim();
        _Company.Phone = txtTel.Value.Trim();
        _Company.Fax = txtFax.Value.Trim();
        _Company.Web = txtWebsite.Value.Trim();
        //Update For Member
        MemberService.UpdateMember(_Member);
        //Update For Company
        CompanyService.UpdateCompany(_Company);
        Response.Redirect("Make_Payment.aspx");



    }

    #endregion
    #region GetSeccsionMember
    private void LoadMember()
    {
        if (Session["MemberId"] != null)
        {
            _Member = MemberService.GetMember(long.Parse(Session["MemberId"].ToString()));
            if (_Member != null)
            {
                Criteria cri = new Criteria();
                cri.add("[" + ADESSA.Entities.Company._Table + "].[" + CompanyColumns.MemberID + "]", _Member.MemberID, Condition.EQUAL, Condition.AND);
                CompanyCollection _CompanyCollection = CompanyService.GetCompanyListByParam("[" + ADESSA.Entities.Company._Table + "].[" + CompanyColumns.CompanyID + "]", OrderDirection.ASC, cri.Criter);
                if (_CompanyCollection.Count > 0)
                {
                    _Company = _CompanyCollection[0];
                    txtConAdd.Value = _Company.Address;
                    txtFax.Value = _Company.Fax;
                    txtTel.Value = _Company.Phone;
                    txtWebsite.Value = _Company.Web;
                    txtZip.Value = _Company.ZipCode;
                    txtNameCom.Value = _Company.CompanyName;
                    ddlSex.SelectedValue = _Member.Sex.ToString();
                    txtNameMem.Value = _Member.FullName;
                    ltrEmailMem.Text = _Member.Email;
                    Country _Country = CountryService.GetCountry(_Company.Country);
                    if (_Country != null)
                    {
                        ltrCountry.Text = _Country.Name;
                    }


                }

            }
        }
        else
        {
            Response.Redirect("MemberLogin.aspx");
        }
    }

    #endregion
    #region LoadShipFree
    private void LoadShipFree()
    {

        Criteria cri = new Criteria();

        MembershipFeeCollection _MembershipFeeCollection = MembershipFeeService.GetMembershipFeeListByParam(MembershipFeeColumns.Other, OrderDirection.ASC, cri.Criter);
        if (_MembershipFeeCollection.Count > 0)
        {
            rpt_ShipFree.DataSource = _MembershipFeeCollection;
            rpt_ShipFree.DataBind();
        }



    }
    protected void rpt_ShipFree_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            return;
        MembershipFee _MembershipFee = (MembershipFee)e.Item.DataItem;
        Literal ltrTitleFree = (Literal)e.Item.FindControl("ltrTitleFree");
        RadioButton rdoFree = (RadioButton)e.Item.FindControl("rdoFree");
        HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
        string script = "SetUniqueRadioButton('rpt_ShipFree.*Portfolios',this)";
        rdoFree.Attributes.Add("onclick", script);
        ltrTitleFree.Text = _MembershipFee.Title;
        hidID.Value = _MembershipFee.MembershipFeeID.ToString();
        int STT = NoCountStart + e.Item.ItemIndex;
        if (Session["MemberId"] != null)
        {
            _Member = MemberService.GetMember(long.Parse(Session["MemberId"].ToString()));
            if (_Member != null)
            {
                if (_Member.MembershipFeeID != 0)
                {
                    if (_MembershipFee.MembershipFeeID == _Member.MembershipFeeID)
                    {
                        rdoFree.Checked = true;
                    }
                }
                else
                {
                    if (STT == 0)
                        rdoFree.Checked = true;
                }
            }
        }
        else
        {
            if (STT == 0)
                rdoFree.Checked = true;
        }

    }

    #endregion
    #region LoadgGender
    private void LoadGender()
    {
        ddlSex.Items.Clear();
        ddlSex.Items.Add(new ListItem("Mr", FetConstant.Mr.ToString()));
        ddlSex.Items.Add(new ListItem("Ms", FetConstant.Ms.ToString()));
        ddlSex.Items.Add(new ListItem("MrMs", FetConstant.MrMs.ToString()));
    }

    #endregion
}
